package Classic;

public class Demo007 {
    static int count=0;
    public static void run(){

        String[][] matrix={
                {"*","*","*","*","*"},
                {"@","@","@","*","@"},
                {"*","@","*","*","@"},
                {"@","@","@","*","@"},
                {"@","@","*","*","@"}
        };
        int[][] matrixFlag=new int[5][5];
        for(int i=0;i<5;i++){
            for(int j=0;j<5;j++){
                count=0;
                find(matrix,matrixFlag,i,j);
                if(count>0){
                    System.out.println("连同区");
                }
            }
        }


        for(int i=0;i<5;i++){
            for(int j=0;j<5;j++){
                System.out.print(matrixFlag[i][j]+" ");
            }
            System.out.println();
        }

        System.out.println(count);
    }

    public static void find(String[][] matrix,int[][] matrixFlag,int row,int col){
        if(row>=0 && row<5 &&
                col>=0 && col<5 &&
                matrix[row][col]=="@" && matrixFlag[row][col]==0){
            count++;
            matrixFlag[row][col]=count;
            find(matrix,matrixFlag,row,col-1);
            find(matrix,matrixFlag,row-1,col-1);
            find(matrix,matrixFlag,row-1,col);
            find(matrix,matrixFlag,row-1,col+1);
            find(matrix,matrixFlag,row,col+1);
            find(matrix,matrixFlag,row+1,col+1);
            find(matrix,matrixFlag,row+1,col);
            find(matrix,matrixFlag,row+1,col-1);

        }
    }
}
